// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal class FormatUtils
    {
        public static void CheckSetting(DataFormatSetting setting, IModelMeta modelMeta)
        {
            var success = BaseCheck(setting, modelMeta);

            if (!success)
            {
                return;
            }

            if (setting.StartProperty >= 0 &&
                modelMeta != null && setting.StartProperty >= modelMeta.PropertyCount)
            {
                throw new IndexOutOfRangeException("The start column specified must be less than end column.");
            }

            if (modelMeta != null && modelMeta.PropertyCount <= setting.DwStartColumn)
            {
                throw new IndexOutOfRangeException("The start column specified must be less than end column.");
            }
        }

        public static void CheckImportSetting(DataFormatSetting setting, IModelMeta modelMeta)
        {
            var success = BaseCheck(setting, modelMeta);

            if (!success)
            {
                return;
            }

            if (modelMeta != null && modelMeta.PropertyCount <= setting.DwStartColumn)
            {
                throw new IndexOutOfRangeException("The start column specified must be less than end column.");
            }
        }

        private static bool BaseCheck(DataFormatSetting setting, IModelMeta modelMeta)
        {
            if (setting == null)
            {
                return false;
            }

            if (setting.StartRow >= 0 && setting.EndRow >= 0
                && setting.StartRow > setting.EndRow)
            {
                throw new ArgumentException("The start row specified must be less than end row.");
            }

            if (setting.StartProperty >= 0 && setting.EndProperty >= 0
                && setting.StartProperty > setting.EndProperty)
            {
                throw new ArgumentException("The start column specified must be less than end column.");
            }

            return true;
        }
    }
}
